#include "ScrollingMenu.h"

ScrollingMenu::ScrollingMenu(ArrayList<string> text,ArrayList<string> value , int _x, int _y, int _colsize, int _rowsize, DrawingCanvas *_canvas)
{
	bgcolor = blue;
	selcolor = red;
	textcolor = black;
	
	charwidth = 7;
	charheight = 14;
	
	colsize = _colsize;
	rowsize = _rowsize;
	x = _x; y = _y;
	canvas = _canvas;
	data = ArrayList<Option>();
	vLines = ArrayList<Text *>();
	bgs = ArrayList<FilledRect *>();
	
	for(int i = 0; i < value.size() && i < text.size(); i++)
		data.add(Option(text.get(i),value.get(i)));
	for(int i = 0; i < rowsize; i++)
	{
		bgs.add(new FilledRect(x,y + (i * charheight),colsize * charwidth,charheight,bgcolor,canvas));
		vLines.add(new Text("",x, y + (i * charheight),textcolor,canvas));
	}
	
	topline = 0;
	curserline = 0;
	
	selectedIndex = 0;
	if(data.size() > 0)
		selected = data.get(0);
	else
		selected = Option("");
	bgs.get(0)->setColor(selcolor);
	redraw();
}
ScrollingMenu::ScrollingMenu(ArrayList<Option> _data,int _x, int _y, int _colsize, int _rowsize,DrawingCanvas *_canvas)
{
	bgcolor = blue;
	selcolor = red;
	textcolor = black;
	
	charwidth = 7;
	charheight = 14;
	
	colsize = _colsize;
	rowsize = _rowsize;
	x = _x; y = _y;
	canvas = _canvas;
	data = _data;
	vLines = ArrayList<Text *>();
	bgs = ArrayList<FilledRect *>();
	
	for(int i = 0; i < rowsize; i++)
	{
		bgs.add(new FilledRect(x,y + (i * charheight),colsize * charwidth,charheight,bgcolor,canvas));
		vLines.add(new Text("",x, y + (i * charheight),textcolor,canvas));
	}
	
	topline = 0;
	curserline = 0;
	
	selectedIndex = 0;
	if(data.size() > 0)
		selected = data.get(0);
	else
		selected = Option("");
	bgs.get(0)->setColor(selcolor);
	redraw();
}
void ScrollingMenu::hide()
{
	for(int i = 0; i < vLines.size(); i++)
		vLines[i]->hide();
}
void ScrollingMenu::show()
{
	for(int i = 0; i < vLines.size(); i++)
		vLines[i]->show();
}
void ScrollingMenu::updateSelected()
{
	selected = data.get(selectedIndex);
}
void ScrollingMenu::moveCurserUp()
{
	if(curserline > 0)
	{
		curserline--;
		selectedIndex = topline + curserline;
		updateSelected();
	}
	else
		scrollUp();
	redraw();
}
void ScrollingMenu::moveCurserDown()
{
	if(curserline + 1 < vLines.size() && selectedIndex + 1 < data.size())
	{
		curserline++;
		selectedIndex = topline + curserline;
		updateSelected();
	}
	else
		scrollDown();
	redraw();
}
void ScrollingMenu::scrollDown()
{
	if(topline + vLines.size() < data.size() && selectedIndex < data.size())
	{
		topline++;
		selectedIndex = curserline + topline;
		updateSelected();
	}
	redraw();
}
void ScrollingMenu::scrollUp()
{
	if(topline > 0 && selectedIndex > 0)
	{
		topline--;
		selectedIndex = topline + curserline;
		updateSelected();
	}
	redraw();
}
void ScrollingMenu::addOption(Option option)
{
	data.add(option);
	redraw();
}
void ScrollingMenu::addOption(string text)
{
	data.add(Option(text));
	redraw();
}
void ScrollingMenu::addOption(string text, string value)
{
	data.add(Option(text,value));
	redraw();
}
void ScrollingMenu::redraw()
{
	for(int i = 0; i < vLines.size(); i++)
	{
		vLines.get(i)->setText("");
		bgs.get(i)->setColor(bgcolor);
	}
	for(int i = 0; i < vLines.size() && i + topline < data.size(); i++)
	{
		string s = data.get(topline + i).text;
		if(s.length() >= colsize)
			s = s.substr(0, colsize);
		vLines.get(i)->setText(s);
	}
	bgs.get(curserline)->setColor(selcolor);
}
void ScrollingMenu::setTextColor(OSL_COLOR color)
{
	for(int i = 0; i < vLines.size(); i++)
		vLines.get(i)->setColor(color);
	textcolor = color;
}
void ScrollingMenu::setSelColor(OSL_COLOR color)
{
	selcolor = color;
	redraw();
}
void ScrollingMenu::setBgColor(OSL_COLOR color)
{
	bgcolor = color;
	redraw();
}
void ScrollingMenu::setOptions(vector<Option> options)
{
	data = ArrayList<Option>(options);
	topline = 0;
	curserline = 0;
	if(data.size() > 0)
		selected = data.get(0);
	else
		selected = Option("");
	selectedIndex = 0;
	redraw();
}
void ScrollingMenu::setOptions(ArrayList<Option> options)
{
	data = options;
	topline = 0;
	curserline = 0;
	if(data.size() > 0)
		selected = data.get(0);
	else
		selected = Option("");
	selectedIndex = 0;
	redraw();
}
